class Tree
    def initialize(parent,  root,  rootType,  leftSubTree,  rightSubTree,  isLeaf)
        @root = root
        @rootType = rootType
        @leftSubTree = leftSubTree
        @rightSubTree = rightSubTree
        @isLeaf = isLeaf
        @parent = parent
    end    
    
    # Returns tikzpicture LaTeX code for the tree
    def getLatex(level=0)       
        distance = if level == 0 then 40 else 20 end
        if isLeaf 
            "node{ #{@root} }"
        else
            "node {#{@root}}[sibling distance=#{distance}mm] child { #{@leftSubTree.getLatex(level+1)} } child {#{@rightSubTree.getLatex(level+1)}}" 
        end
    end
    
    def getInfix()
        if isLeaf then "#{@root}" else "(#{@leftSubTree.getInfix} #{@root} #{@rightSubTree.getInfix})" end        
    end
    
    def getPostfix()
        if isLeaf then "#{@root}" else "#{@leftSubTree.getPostfix} #{@rightSubTree.getPostfix} #{@root}" end        
    end   
    
    def getPrefix()
        if isLeaf then "#{@root}" else "#{@root} #{@leftSubTree.getPrefix} #{@rightSubTree.getPrefix}" end        
    end    
    
    def eval()
        case @rootType
            when TokenType::INTEGER then return Integer(@root)
            when TokenType::PLUS_OP then return @leftSubTree.eval+@rightSubTree.eval
            when TokenType::MINUS_OP then return @leftSubTree.eval-@rightSubTree.eval
            when TokenType::MULT_OP then return @leftSubTree.eval*@rightSubTree.eval
        end
    end 
    
    def isLeaf
        @isLeaf
    end
    
    def isOk
        if(isLeaf and @root != nil)
            return true
        end
        if(not isLeaf and @leftSubTree != nil and @leftSubTree != nil)
            return (@leftSubTree.isOk and @rightSubTree.isOk)
        end
        return false
    end
end
